package org.shreker.shan.base.queryobject;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class QueryObject {
	
	private Integer pageNum = 1; // 当前页面
	private Integer pageSize = 10; // 页面容量
	private Integer firstPage = 1; // 第一页
	private Integer pageCount; // 总页数
	private Integer prePage; // 上一页
	private Integer nextPage; // 下一页
	
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	/**
	 * 计算页面信息
	 */
	public void calcPageInfo(Integer recordCount) {
		// 计算末页: 必须知道数据库的总记录数
		pageCount = (recordCount + pageSize - 1) / pageSize;

		if (pageNum < 1) {
			pageNum = 1;
		}
		if (pageNum > pageCount) {
			pageNum = pageCount;
		}

		// 计算上一页
		prePage = pageNum - 1 <= 0 ? 1 : pageNum - 1;
		// 计算下一页
		nextPage = pageNum + 1 > pageCount ? pageCount : pageNum + 1;
	}

	public Integer getBegin() {
		return (pageNum - 1) * pageSize;
	}

	@Override
	public String toString() {
		return "QueryObject [pageNum=" + pageNum + ", pageSize=" + pageSize + ", firstPage=" + firstPage + ", pageCount=" + pageCount + ", prePage="
				+ prePage + ", nextPage=" + nextPage + "]";
	}
	
}
